<?php

class utils
{

    public static function invokeOperator($needle, $operator, $params)
    {
        switch ($operator)
        {
            case 'equal':
                return ($needle == $params[0]);

            case 'not_equal':
                return ($needle != $params[0]);

            case 'less_than':
                return ($needle < $params[0]);

            case 'less_than_equal':
                return ($needle <= $params[0]);

            case 'greater_than':
                return ($needle > $params[0]);

            case 'greater_than_equal':
                return ($needle >= $params[0]);

            case 'between':
                return ($needle > $params[0] && $needle > $params[1]);

            case 'in':
                return in_array($needle, $params);

            case 'not_in':
                return !in_array($needle, $params);
        }
        throw new Exception("can not invoke operator {$operator}");
    }

    public static function invokeDateOperator($needle, $operator, $params)
    {
        switch ($operator)
        {
            case 'equal':
                return (strtotime($needle) == strtotime($params[0]));

            case 'not_equal':
                return (strtotime($needle) != strtotime($params[0]));

            case 'between':
                $needle = strtotime($needle);
                $min = strtotime($params[0]);
                $max = strtotime($params[1]);
                return ($needle >= $min && $needle <= $max);

            case 'less_than':
                return (strtotime($needle) < strtotime($params[0]));

            case 'less_than_equal':
                return (strtotime($needle) <= strtotime($params[0]));

            case 'greater_than':
                return (strtotime($needle) > strtotime($params[0]));

            case 'greater_than_equal':
                return (strtotime($needle) >= strtotime($params[0]));

            case 'in':                
                 return in_array($needle, $params);
                
            case 'not_in':
                 return !in_array($needle, $params);
        }
        throw new Exception("can not invoke operator {$operator}");
    }

    public static function log($str)
    {
        $log = KLogger::instance(ROOT_DATA_LOG_FILE, KLogger::DEBUG);
        $log->logInfo($str);
    }

}

?>
